# _*_ coding:utf-8 _*_
# @Time      : 8:24
# @Author    :baizhou
import cv2
import numpy as np
from django.conf import settings
import os
class FeatureExtract(object):
    def huInvariantMoment(self, path):
        '''
        提取图像的Hu不变矩
        :param path: 图像的路径
        :return: 返回不变矩的列表
        '''
        img = cv2.imread(path, 0)
        moments = cv2.moments(img)
        humoments = cv2.HuMoments(moments)
        # humoments = no.log(np.abs(humoments)) # 同样建议取对数

        data = {
            'hu1': str(humoments[0][0]),
            'hu2': str(humoments[1][0]),
            'hu3': str(humoments[2][0]),
            'hu4': str(humoments[3][0]),
            'hu5': str(humoments[4][0]),
            'hu6': str(humoments[5][0]),
            'hu7': str(humoments[6][0])

        }

        return data

    def colorExtract(self, path):

        '''
        提取颜色特征
        :param path:路径
        :return: 返回提取的颜色分量的均值与方差
        '''
        imgRGB = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2RGB)
        imgHSV = cv2.cvtColor(cv2.imread(path), cv2.COLOR_BGR2HSV)
        '''
        RMean, Rstd, GMean, Gstd, BMean, Bstd, HMean, Hstd, SMean, Sstd, VMean, Vstd
        '''
        RMean, Rstd = (np.mean(imgRGB[:, :, 0]), np.std(imgRGB[:, :, 0]))
        GMean, Gstd = (np.mean(imgRGB[:, :, 1]), np.std(imgRGB[:, :, 2]))
        BMean, Bstd = (np.mean(imgRGB[:, :, 2]), np.std(imgRGB[:, :, 2]))
        HMean, Hstd = (np.mean(imgHSV[:, :, 0]), np.std(imgHSV[:, :, 0]))
        SMean, Sstd = (np.mean(imgHSV[:, :, 1]), np.std(imgHSV[:, :, 1]))
        VMean, Vstd = (np.mean(imgHSV[:, :, 2]), np.std(imgHSV[:, :, 2]))
        data = {
            'RMean': str(RMean),
            'Rstd': str(Rstd),
            'GMean': str(GMean),
            'Gstd': str(Gstd),
            'BMean': str(BMean),
            'Bstd': str(Bstd),
            'HMean': str(HMean),
            'Hstd': str(Hstd),
            'SMean': str(SMean),
            'Sstd': str(Sstd),
            'VMean': str(VMean),
            'Vstd': str(Vstd)
        }
        return data
